package com.example.xui.BlankFragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.xui.R;
import com.example.xui.View.Login.LoginView;
import com.github.florent37.materialtextfield.MaterialTextField;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class BlankFragment6 extends Fragment{
    private Button mbutton;
    BlankFragment7 f4=new BlankFragment7();
    private FragmentManager manager;
    private FragmentTransaction ft;
    private EditText editText;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank6, container, false);
    }
    public void init(){
        mbutton=getActivity().findViewById(R.id.mbutton4);
        editText=getActivity().findViewById(R.id.editText);
    }
    public void onActivityCreated(@Nullable Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        init();
        manager=getFragmentManager();
        mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        RequestParams params = new RequestParams("http://10.138.87.49:8080/login/logininitTel");
                        params.addHeader("Content-Type","application/x-www-form-urlencoded");
                        params.addBodyParameter("loginTel",editText.getText().toString());
                        x.http().post(params, new Callback.CacheCallback<String>() {
                            @Override
                            public boolean onCache(String result) {
                                return false;
                            }

                            @Override
                            public void onSuccess(String result) {
                                if(result.equals("success")){
                                    Bundle bundle=new Bundle();
                                    bundle.putString("phone",editText.getText().toString());
                                    f4.setArguments(bundle);
                                    ft=manager.beginTransaction();
                                    ft.replace(R.id.framgement6,f4);
                                    ft.addToBackStack(null);
                                    ft.commit();
                                    ConstraintLayout constraintLayout=getActivity().findViewById(R.id.mconstraintlayout1);
                                    constraintLayout.setVisibility(View.INVISIBLE);
                                }
                                else
                                    Toast.makeText(getContext(),"你输入的手机号未注册",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                Toast.makeText(getContext(),"网络请求错误",Toast.LENGTH_SHORT).show();
                                Log.i("re",params.toString());
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {
                                Toast.makeText(getContext(),"请求已取消",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFinished() {
                            }
                        });
                    }
                }.start();

            }
        });
    }


}
